package com.h3bpm.kumhosunny.gateway.controller;

import OThinker.Common.DotNetToJavaStringHelper;
import OThinker.H3.Controller.ControllerBase;
import OThinker.H3.Controller.ViewModels.ActionResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.h3bpm.base.util.SqlUtils;
import com.h3bpm.kumhosunny.common.utils.DateTableUtil;
import com.h3bpm.kumhosunny.common.utils.NormalUtil;
import com.h3bpm.kumhosunny.common.utils.SqlUtil;
import com.h3bpm.kumhosunny.gateway.beans.PageSearchInfo;
import data.DataTable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhengyj
 * @date 2019/9/17
 * @description 门户控制器
 */
@Controller
@RequestMapping("/Portal/kumhosunny/gateway")
public class GatewayController extends ControllerBase {

        private static final Logger LOGGER = LoggerFactory.getLogger(ControllerBase.class);

    private ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public String getFunctionCode() {
        return null;
    }



    /**
     *  获取通知公告 列表
     *
     * @return
     */
    @RequestMapping(value = "/loadNoticeData", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public ActionResult loadNoticeData(PageSearchInfo pageSearchInfo) {

        try {
            LOGGER.warn("======获取通知公告列表=======param:" + objectMapper.writeValueAsString(pageSearchInfo));
        } catch (JsonProcessingException e) {

        }

        ActionResult actionResult = new ActionResult(true);
        int start = pageSearchInfo.getPageStart();
        int end = start + pageSearchInfo.getPageSize();
        try {
            String searchKey = pageSearchInfo.getSearchKey();

            String whereSql = "";
            // title 条件查询
            if (!DotNetToJavaStringHelper.isNullOrEmpty(searchKey)) {
                whereSql = "and a.title like '%" + searchKey + "%' ";
            }

            // 查询的sql   需要三个参数， 1 查询字段  2  查询条件 3 分页limit
            String querySql = "SELECT %s from i_notice a INNER JOIN ot_instancecontext b on a.ObjectID = b.BizObjectId and b.State = 4 %s  ORDER BY a.setTop DESC ,b.FinishTime desc %s ";

            // 先查询 总数
            String queryCount = String.format(querySql, "count(*)", whereSql,"");
            LOGGER.info("=====查询通知公告总数sql====" + queryCount);
            DataTable dataTable = SqlUtil.doQuery(queryCount);
            actionResult.setLoadData(dataTable.getRows().size() > 0 ? dataTable.getRows().get(0).getString(0) : "0");
            // 再查询内容
            String queryColumn = "a.title,a.objectid,b.OriginatorName,b.FinishTime";
            String query = String.format(querySql, queryColumn, whereSql,String.format("limit %s,%s",start,end));
            LOGGER.info("=====查询通知公告列表sql 带分页====" + query);
            dataTable = SqlUtil.doQuery(query);
            actionResult.setExtend(DateTableUtil.getRowList(dataTable));
        } catch (Exception e) {
            actionResult.setSuccess(false);
            actionResult.setMessage(e.toString());
        }
        return actionResult;
    }


    /**
     * 获取 通知公告 详细信息 带HTML
     *
     *
     * @param objectId
     * @return
     */
    @RequestMapping(value = "/LoadDataInfo", produces = "application/json;charset=utf-8", method = RequestMethod.GET)
    @ResponseBody
    public ActionResult loadDataInfo( String objectId) {
        LOGGER.warn("============获取 通知公告 详细信息========objectId" + objectId);
        ActionResult actionResult = new ActionResult(true);
        if (this.getUserValidator() == null) {
            actionResult.setSuccess(false);
            actionResult.setMessage("用户未登陆");
        }

        String queryStr = "SELECT a.title,a.content,b.OriginatorName,b.FinishTime from i_notice a INNER JOIN ot_instancecontext b on a.ObjectID = b.BizObjectId and   a.ObjectID = '%s'";

        try {
            Map<String, Object> resultMap = new HashMap<>(2);
            resultMap.put("mainData", DateTableUtil.dataTableParseBlobToString(SqlUtil.doQuery(String.format(queryStr,objectId))));
            resultMap.put("attach", NormalUtil.getAttachments(objectId, "Attachment"));
            actionResult.setExtend(resultMap);
        } catch (Exception e) {
            LOGGER.error("====LoadDataInfo=====" + e);
            actionResult.setSuccess(false);
            actionResult.setMessage("=====LoadDataInfo=====" + e);
        }

        return actionResult;
    }


}
